// import useWebsocketStore from '@store/webSocket'

import { useyanZhengStore } from '@/stores/modules/yanZheng'
import { useWebsocketStore } from '@/stores/modules/websocket'

// const WebsocketStore = useWebsocketStore()
const wsUrl1 = ''
const wsUrl2 = ''
const wsUrl = ref('')
// const wsUrl = 'ws://192.168.120.254:300/tea/websocket' //   茶
// const wsUrl = 'ws://192.168.120.254:300/packeges/websocket' //   包
// const wsUrl = 'ws://192.168.120.254:300/packeges/websocket' //   衣服
// const wsUrl = 'ws://192.168.120.254:300/shoes/websocket' //   鞋子
// const wsUrl = 'ws://192.168.120.254:300/lang/websocket' //   郎酒
// const wsUrl = 'ws://192.168.120.254:300/mike/websocket' //   牛奶
// const wsUrl = 'ws://192.168.120.254:302/websocket' //   立邦
let ws: WebSocket | null = null
const reconnectTimer: any = ref(null)
const reconnectInterval = 60000 // 重连间隔时间，例如 60 秒
// 创建 WebSocket 连接
export function connectWebSocket(wsUrl1: string) {
  wsUrl.value = wsUrl1
  // wsUrl1 = 'ws://192.168.120.4:2001/websocket' // 水
  // wsUrl2 = 'ws://192.168.120.4:18083/websocket/session/tableName'
  // if (useyanZhengStore().index == 'water') {
  // } else {
  // }
  // else if (indexUrl == 'maotai') {
  //   wsUrl = 'ws://192.168.120.4:18083/websocket/session/maotai'
  // } else if (indexUrl == 'cha') {
  //   wsUrl = 'ws://192.168.120.4:18083/websocket/session/cha'
  // } else if (indexUrl == 'bao') {
  //   wsUrl = 'ws://192.168.120.4:18083/websocket/session/bao'
  // } else if (indexUrl == 'yifu') {
  //   wsUrl = 'ws://192.168.120.4:18083/websocket/session/yifu'
  // } else if (indexUrl == 'xie') {
  //   wsUrl = 'ws://192.168.120.4:18083/websocket/session/xie'
  // } else if (indexUrl == 'lang') {
  //   wsUrl = 'ws://192.168.120.4:18083/websocket/session/lang'
  // } else if (indexUrl == 'niunai') {
  //   wsUrl = 'ws://192.168.120.4:18083/websocket/session/niunai'
  // } else if (indexUrl == 'jiancai') {
  //   wsUrl = 'ws://192.168.120.4:18083/websocket/session/jiancai'
  // } else if (indexUrl == 'yao') {
  //   wsUrl = 'ws://192.168.120.4:18083/websocket/session/yao'
  // }
  console.log('Connecting to WebSocket URL:', wsUrl1) // 添加调试信息
  // ws = new WebSocket(wsUrl1)
  ws = new WebSocket(wsUrl.value)
  ws.onopen = function () {
    if (reconnectTimer.value) clearInterval(reconnectTimer.value) // 清除重连计时器
    // WebsocketStore.setStatus(Date.now())
    console.log('WebSocket 连接成功')
  }

  ws.onmessage = function (event) {
    if (reconnectTimer.value) clearInterval(reconnectTimer.value) // 清除重连计时器
    if (event.data) {
      const data = event.data.split(',')
      const index = data.length == 3 ? data[2] : data[1]
      let times = 0
      console.log(useyanZhengStore().index !== index, useyanZhengStore().index, index)
      if (useyanZhengStore().index !== index) {
        // closeWebSocket()
        // connectWebSocket()
        useyanZhengStore().index = index
        useWebsocketStore().status = data[0]
        useWebsocketStore().date = Math.random()
        times = 2000
      }
      if (data[0] == 'add' || data[0].includes('del')) {
        console.log('新增')
        useWebsocketStore().status = data[0]
        useWebsocketStore().date = Math.random()
      } else {
        console.log('修改', event.data)
        const data2 = data.slice(0, 2).map((item: any) => {
          return item.split(':')[1]
        })
        setTimeout(() => {
          useyanZhengStore().setProfile(data2)
        }, times)
        // WebsocketStore.setStatus(Math.random())
      }
    }
  }

  ws.onerror = function (error) {
    tryReconnect() // 尝试重连
  }

  ws.onclose = () => {
    // tryReconnect() // 尝试重连
  }
}

// 关闭 WebSocket 连接
export function closeWebSocket() {
  if (ws) {
    ws.close()
    console.log('WebSocket 连接已关闭')
  }
}

// 重连函数
function tryReconnect() {
  if (reconnectTimer.value) clearInterval(reconnectTimer.value) // 清除现有的计时器
  reconnectTimer.value = setInterval(() => {
    // connectWebSocket(wsUrl.value)
  }, reconnectInterval)
}

export default {
  connectWebSocket,
}
